home *** CD-ROM | disk | FTP | other *** search
/ The Best Shareware of the Year 1997 Winter / The Best Shareware of the Year 1997 Winter.iso / dist / wm / wmfiles / 9605may / progwin / code.txt
Encoding:
Text File  |  1996-02-06  |  4.9 KB  |  148 lines

  1. //Web page retrieval with Sweeper Internet and Http functions (from
  2. SurfBear sample)
  3.  
  4. //
  5. // This is where all of the actually Internet work is done.
  6. //
  7. UINT CInternetThread::_GetPageWorker()
  8. {
  9.     UINT uiResult = THREAD_BAD;
  10.  
  11.     if (m_hSession == NULL)
  12.    {
  13.       //
  14.       // Initialize the Internet Functions.
  15.       //
  16.       TRACE("Starting Session (Access = %i) (Proxy = %s)\r\n",
  17.             GetAccessTypeIndex(),
  18.             (LPCTSTR)m_strProxyServer) ;
  19.       m_hSession = ::InternetOpen("MSDN SurfBear",
  20.                                   m_dwAccessType,
  21.                                   m_strProxyServer,
  22.                                   INVALID_PORT_NUMBER,
  23.                                   0 ) ;
  24.  
  25.       if (!Succeeded(m_hSession, "InternetOpen"))
  26.       {
  27.          // Send message to UI that we finished.
  28.          ::PostMessage(m_hPostMsgWnd,WM_READFILECOMPLETED, NULL,
  29. (LPARAM)THREAD_BAD) ;
  30.  
  31.          return THREAD_BAD;
  32.       }
  33.    }
  34.  
  35.    HINTERNET hConnect = ::InternetConnect(m_hSession,
  36.                                           m_strServer,
  37.                                           INVALID_PORT_NUMBER,
  38.                                           "",
  39.                                           "",
  40.                                           INTERNET_SERVICE_HTTP,
  41.                                           0,
  42.                                           0) ;
  43.  
  44.                                           
  45.    if (Succeeded(hConnect, "InternetConnect"))
  46.    {
  47.  
  48.       HINTERNET hHttpFile = ::HttpOpenRequest(hConnect,
  49.                                               "GET",
  50.                                               m_strPath,
  51.                                               HTTP_VERSION,
  52.                                               "",
  53.                                               0,
  54.                                               INTERNET_OPEN_FLAG_NO_CACHE,
  55.                                               0) ;
  56.  
  57.       if (Succeeded(hHttpFile, "HttpOpenRequest"))
  58.       {
  59.          
  60.          BOOL bSendRequest = ::HttpSendRequest(hHttpFile, "", 0, 0, 0);
  61.          
  62.          if (Succeeded((HINTERNET)bSendRequest, "HttpSendRequest"))
  63.          {
  64.             // Get size of file.
  65.             char bufQuery[32] ;
  66.             DWORD dwFileSize ;
  67.             DWORD dwLengthBufQuery = sizeof (bufQuery);
  68.             BOOL bQuery = ::HttpQueryInfo(hHttpFile,
  69.                            HTTP_QUERY_CONTENT_LENGTH,
  70.                            bufQuery,
  71.                            &dwLengthBufQuery) ;
  72.             if (Succeeded((HINTERNET)bQuery, "HttpQueryInfo"))
  73.             {
  74.                // The Query was successful, so allocate the memory.
  75.                TRACE("HttpQueryInfo FileSize is %s.\r\n", bufQuery) ;
  76.                dwFileSize = (DWORD)atol(bufQuery) ;
  77.             }
  78.             else
  79.             {
  80.                // The Query failed. Allocate some memory. Should
  81. allocate memory in blocks.
  82.                TRACE("\tQueryInfo Failed. Just get 5k.\r\n") ;
  83.                dwFileSize = 5*1024 ;
  84.             }
  85.  
  86.             ASSERT(m_buffer == NULL);
  87.             m_buffer = new char[dwFileSize+1] ;
  88.             DWORD dwBytesRead ;
  89.             BOOL bRead = ::InternetReadFile(hHttpFile,    
  90.                                             m_buffer,    
  91.                                             dwFileSize+1,     
  92.                                             &dwBytesRead);    
  93.             if (Succeeded((HINTERNET)bRead, "InternetReadFile"))
  94.             {
  95.                TRACE("\tBytes Read is %d\r\n", dwBytesRead) ;
  96.                m_buffer[dwBytesRead] = 0 ;
  97.                uiResult = THREAD_GOOD;
  98.             } // InternetReadFile
  99.          } // HttpSendRequest
  100.  
  101.          VERIFY(::InternetCloseHandle(hHttpFile));
  102.       } // HttpOpenRequest
  103.  
  104.       VERIFY(::InternetCloseHandle(hConnect)) ;
  105.    } // InternetConnect
  106.  
  107.    ::PostMessage(m_hPostMsgWnd,WM_READFILECOMPLETED, NULL, (LPARAM)uiResult) ;
  108.  
  109.    return uiResult ;
  110. }
  111.  
  112. ///Web page, ftp file, or gopher file retrieval with Sweeper URL
  113. Moniker (from Progress sample)
  114.  
  115. // 
  116. -----------------------------------------------------------------------
  117. --- -
  118. // %%Function: CDownload::DoDownload
  119. // 
  120. -----------------------------------------------------------------------
  121. --- -
  122.  HRESULT
  123. CDownload::DoDownload(HWND hwndStatus, HWND hwndProgress, HWND hwndText)
  124. {
  125.     HRESULT hr =  CreateURLMoniker(NULL, m_url, &m_pmk);
  126.  
  127.     if( SUCCEEDED(hr) )
  128.         {
  129.         m_pbsc = new CBindStatusCallback(hwndStatus, hwndProgress, hwndText);
  130.         if (m_pbsc != NULL)
  131.             m_pbsc->AddRef();
  132.         else
  133.             hr = E_OUTOFMEMORY;
  134.     }
  135.  
  136.     if (SUCCEEDED(hr))
  137.         hr = CreateBindCtx(0, &m_pbc);
  138.  
  139.     if (SUCCEEDED(hr))
  140.         hr = m_pbc->RegisterObjectParam(OLESTR("BindStatusCallback"), m_pbsc);
  141.         
  142.     IStream* pstm;
  143.     if (SUCCEEDED(hr))
  144.         hr = m_pmk->BindToStorage(m_pbc, 0, IID_IStream, (void**)&pstm);
  145.  
  146.     return hr;
  147. }  // CDownload::DoDownload
  148.